package top.heliming.leyou.userservice.service.impl;

import java.util.List;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.heliming.leyou.userservice.bean.User;
import top.heliming.leyou.userservice.dao.UserMapper;
import top.heliming.leyou.userservice.service.UserService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/05/22 上午 1:48
 */
@Service
public class UserServiceImpl implements UserService {
  private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);


  @Autowired
  private UserMapper userMapper;

  /*改造服务提供者以触发熔断*/
  @Override
  public User queryById(Long id) throws InterruptedException {
    long begin = System.currentTimeMillis();
    // 为了演示超时现象，我们在这里然线程休眠,时间随机 0~2000毫秒
    Thread.sleep(new Random().nextInt(2000));
    User user = this.userMapper.selectByPrimaryKey(id);
    long end = System.currentTimeMillis();
    // 记录访问用时：
    logger.info("访问用时：{}", end - begin);
    return user;
  }

  @Override
  @Transactional
  public void deleteById(Long id) {
    this.userMapper.deleteByPrimaryKey(id);

  }

  @Override
  public List<User> queryAll() {
    return userMapper.selectAll();
  }
}
